package com.ksd.modules.download.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.exception.ApiException;
import com.ksd.modules.download.entity.AdminDownloadsUserBuy;
import com.ksd.modules.download.mapper.AdminDownloadsUserBuyMapper;
import com.ksd.modules.download.po.AdminDownloadsUserBuySavePO;
import com.ksd.modules.download.po.AdminDownloadsUserBuySearchPO;
import com.ksd.modules.download.service.IAdminDownloadsService;
import com.ksd.modules.download.service.IAdminDownloadsUserBuyService;
import com.ksd.modules.download.vo.AdminDownloadsUserBuyVo;
import com.ksd.modules.download.vo.AdminDownloadsVo;
import com.ksd.modules.members.service.IAdminUserService;
import com.ksd.modules.members.vo.AdminUserVo;
import com.ksd.utils.snow.SnowflakeIdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminDownloadsUserBuyServiceImpl extends BaseServiceImpl<AdminDownloadsUserBuyMapper, AdminDownloadsUserBuy> implements IAdminDownloadsUserBuyService {

    @Autowired
    private IAdminUserService adminUserService;
    @Autowired
    private IAdminDownloadsService adminDownloadsService;

    /**
     * 查询列表
     *
     * @param adminDownloadsUserBuySearchPO
     * @return
     */
    public Page<AdminDownloadsUserBuyVo> findAdminDownloadsUserBuys(AdminDownloadsUserBuySearchPO adminDownloadsUserBuySearchPO) {
        Page<AdminDownloadsUserBuy> page = new Page<>(adminDownloadsUserBuySearchPO.getPageNo(), adminDownloadsUserBuySearchPO.getPageSize());
        LambdaQueryWrapper<AdminDownloadsUserBuy> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminDownloadsUserBuy::getSystemId, SystemIdThrealLocal.get().getSystemId());
        // 订单编号
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminDownloadsUserBuySearchPO.getOrderno()), AdminDownloadsUserBuy::getOrderno, adminDownloadsUserBuySearchPO.getOrderno());
        // 支付方式
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsUserBuySearchPO.getPayMethod()), AdminDownloadsUserBuy::getPayMethod, adminDownloadsUserBuySearchPO.getPayMethod());
        // 具体资源订单
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsUserBuySearchPO.getOpid()), AdminDownloadsUserBuy::getOpid, adminDownloadsUserBuySearchPO.getOpid());
        // 兑换码
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminDownloadsUserBuySearchPO.getCode()), AdminDownloadsUserBuy::getCode, adminDownloadsUserBuySearchPO.getCode());
        // uuid
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminDownloadsUserBuySearchPO.getUuid()), AdminDownloadsUserBuy::getUuid, adminDownloadsUserBuySearchPO.getUuid());
        // 手机
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminDownloadsUserBuySearchPO.getPhone()), AdminDownloadsUserBuy::getPhone, adminDownloadsUserBuySearchPO.getPhone());
        // 模糊匹配
        if (StringUtils.isNotEmpty(adminDownloadsUserBuySearchPO.getKeyword())) {
            lambdaQueryWrapper.and(c -> c.like(
                            AdminDownloadsUserBuy::getNickname, adminDownloadsUserBuySearchPO.getKeyword())
                    .or().like(AdminDownloadsUserBuy::getUsername, adminDownloadsUserBuySearchPO.getKeyword())
                    .or().like(AdminDownloadsUserBuy::getTitle, adminDownloadsUserBuySearchPO.getKeyword())

            );
        }
        // 排序
        lambdaQueryWrapper.orderByDesc(AdminDownloadsUserBuy::getCreateTime);
        Page<AdminDownloadsUserBuy> adminDownloadsPage = this.page(page, lambdaQueryWrapper);
        Page<AdminDownloadsUserBuyVo> adminDownloadsVoPage = tranferPageBo(adminDownloadsPage, AdminDownloadsUserBuyVo.class);
        return adminDownloadsVoPage;
    }

    /**
     * 保存和更新
     *
     * @param adminDownloadsUserBuySavePO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AdminDownloadsUserBuyVo saveUpdateAdminDownloadsUserBuy(AdminDownloadsUserBuySavePO adminDownloadsUserBuySavePO) {
        AdminDownloadsUserBuy adminDownloads = tranferBo(adminDownloadsUserBuySavePO, AdminDownloadsUserBuy.class);
        adminDownloads.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminDownloads.setSystemName(SystemIdThrealLocal.get().getSystemName());
        this.saveOrUpdate(adminDownloads);
        AdminDownloadsUserBuyVo adminDownloadsVo = tranferBo(adminDownloads, AdminDownloadsUserBuyVo.class);
        return adminDownloadsVo;
    }

    /**
     * 根据ID获取明细
     *
     * @param id
     * @return
     */
    public AdminDownloadsUserBuyVo getAdminDownloadsUserBuyById(Long id) {
        AdminDownloadsUserBuy adminDownloads = this.getById(id);
        AdminDownloadsUserBuyVo adminDownloadsVo = tranferBo(adminDownloads, AdminDownloadsUserBuyVo.class);
        return adminDownloadsVo;
    }

    /**
     * 单个物理删除
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeAdminDownloadsUserBuyById(Long id) {
        boolean b = this.removeById(id);
        return b;
    }


    /**
     * 批量物理删除
     *
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchRemovesAdminDownloadsUserBuy(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }


    /**
     * 修改状态
     *
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStatus(Long id, String column, Integer value) {
        boolean b = super.updateStatus(id, column, value);
        return b;
    }

    /**
     * 修改状态
     *
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    public boolean updateStatusStr(Long id, String column, String value) {
        boolean b = super.updateStatusStr(id, column, value);
        return b;
    }

    /**
     * 用户绑定下载订单
     *
     * @param uuid
     * @param opid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean bindUserOrder(String uuid, Long opid) {
        LambdaQueryWrapper<AdminDownloadsUserBuy> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminDownloadsUserBuy::getUuid, uuid);
        lambdaQueryWrapper.eq(AdminDownloadsUserBuy::getOpid, opid);
        AdminDownloadsUserBuy adminDownloadsUserBuy = this.getOne(lambdaQueryWrapper);
        if (adminDownloadsUserBuy != null) {
            throw new ApiException(501, "该用户已绑定此资源!!!");
        }
        adminDownloadsUserBuy = new AdminDownloadsUserBuy();
        AdminUserVo adminUserVo = adminUserService.getAdminUserByUUId(uuid);
        AdminDownloadsVo adminDownloadsVo = adminDownloadsService.getAdminDownloadsById(opid);
        adminDownloadsUserBuy.setOpid(opid);
        adminDownloadsUserBuy.setUserId(adminUserVo.getId());
        adminDownloadsUserBuy.setNickname(adminUserVo.getNickName());
        adminDownloadsUserBuy.setUsername(adminUserVo.getUserName());
        adminDownloadsUserBuy.setAvatar(adminUserVo.getAvatar());
        adminDownloadsUserBuy.setUuid(adminUserVo.getUuid());
        adminDownloadsUserBuy.setPhone(adminUserVo.getTelephone());
        adminDownloadsUserBuy.setAddress(adminUserVo.getAddress());

        adminDownloadsUserBuy.setTitle(adminDownloadsVo.getTitle());
        adminDownloadsUserBuy.setDescription(adminDownloadsVo.getDescription());
        adminDownloadsUserBuy.setCover(adminDownloadsVo.getImg());
        adminDownloadsUserBuy.setCode("");
        adminDownloadsUserBuy.setPrice(Objects.nonNull(adminDownloadsVo.getPrice()) ? adminDownloadsVo.getPrice() : String.valueOf(adminDownloadsVo.getCoin()));

        adminDownloadsUserBuy.setOrderno(String.valueOf(new SnowflakeIdWorker(7, 8).nextId()));
        adminDownloadsUserBuy.setTradeno(String.valueOf(new SnowflakeIdWorker(8, 8).nextId()));
        adminDownloadsUserBuy.setPayMethod(4);
        adminDownloadsUserBuy.setPayMethodName("人工");

        adminDownloadsUserBuy.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminDownloadsUserBuy.setSystemName(SystemIdThrealLocal.get().getSystemName());
        adminDownloadsUserBuy.setOrderJson("");
        return this.saveOrUpdate(adminDownloadsUserBuy);
    }
}
